package com.fairytask.engine.components.list
{
	import com.fairytask.engine.components.button.ButtonState;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * list条目 
	 * @author hanjy
	 * 
	 */	
	public class ListItem extends Sprite
	{
		//容器
		private var _bgSpace		:Sprite;
		private var _iconSpace		:Sprite;
		private var _txtLabel		:Label;
		
		//临时改变一下文本
		private var _txtFeild		:TextField;
		
		private var _normalMC		:MovieClip;
		private var _overMC		:MovieClip;
		private var _downMC		:MovieClip;
		private var _unallowed		:MovieClip;
		private var _hotArea		:Sprite;
		private var _text			:Text;
		
		//皮肤
		private var _skin			:String	=	"";
		private var _icon			:DisplayObject;
		
		//设定
		private var _itemWidth		:Number	=	0;
		private var _itemHeight	:Number	=	0;
		private var _color			:String	=	"FFCC00";
		
		//数据
		private var _id			:int	=	0;
		private var _label			:String	=	"";
		private var _listData		:Object;
		private var _layOut		:String;
		
		//属性
		private var _enabled		:Boolean	=	true;
		//按钮当前状态
		private var _currentState	:String = ButtonState.NORMAL;
		
		public function ListItem(str:String,w:Number,h:Number,itemSkin:String="",ld:Object=null,layOut:String="left",text:String="")
		{
			super();
			_itemWidth	=	w;
			_itemHeight	=	h;
			_layOut		= 	layOut;
			initGUI(str,text);
			initEvent();
			skin		=	itemSkin;
			buttonMode	=	true;
			_listData		=	ld;
		}
		
		/**
		 * 初始化显示 
		 * @param str
		 * 
		 */
		private function initGUI(str:String,text:String):void
		{
			_label				=	str;
			
			_txtFeild			=	new TextField();
			var _tf	:TextFormat	=	new TextFormat();
			_tf.font		=	"微软雅黑";
			_tf.size = 12;
			_txtFeild.setTextFormat(_tf);
			_txtFeild.defaultTextFormat = _tf;
			_txtFeild.selectable	=	false;
			_txtFeild.multiline	=	true;
			_txtFeild.textColor	=	0xFFCC00;
			_txtFeild.autoSize			=	TextFieldAutoSize.LEFT;
			_txtFeild.type				=	TextFieldType.DYNAMIC;
			_txtFeild.width		=	_itemWidth;
			_txtFeild.htmlText	=	str;
			//_txtFeild.height	=	_txtFeild.textHeight;
			_txtFeild.height	= 20;
		
			/*_txtLabel			=	new Label();
			_txtLabel.color		=	_color;
			_txtLabel.htmlStrByBorder	=	str;*/
			_bgSpace			=	new Sprite();
			
			_hotArea			=	new Sprite();
			
			_iconSpace			=	new Sprite();
			
			//_txtLabel.mouseEnabled	=	
			_iconSpace.mouseEnabled	=	false;
			
			_bgSpace.x=_hotArea.x=_iconSpace.x
			_bgSpace.y=_hotArea.y=_iconSpace.y
			addChild(_bgSpace);
			addChild(_txtFeild);
			addChild(_iconSpace);
			
			addChild(_hotArea);
			
			drawBG();
			
			if(_layOut=="center")
			{
				alignTxt();
			}
			if(_layOut=="left")
			{
				alignLeft();
			}
			
			if(text!="")
			{
				_text	=	new Text(100,80,"","");
				_text.htmlStr	=	text;
				_text.y	=	30;
				_text.mouseEnabled	=	true;
				addChild(_text);
			}
			
//			drawHotArea();
		}
		
		/**
		 *  添加事件
		 */
		private function initEvent():void
		{
			addEventListener(MouseEvent.ROLL_OVER,showOver);
			addEventListener(MouseEvent.ROLL_OUT,showOut);
			addEventListener(MouseEvent.MOUSE_DOWN,showDown);
			addEventListener(MouseEvent.MOUSE_UP,showUP);
		}
		
		/**
		 * 移除事件 
		 */
		private function removeEvent():void
		{
			removeEventListener(MouseEvent.ROLL_OVER,showOver);
			removeEventListener(MouseEvent.ROLL_OUT,showOut);
			removeEventListener(MouseEvent.MOUSE_DOWN,showDown);
			removeEventListener(MouseEvent.MOUSE_UP,showUP);
		}
		
		/**
		 * 画背景 
		 * @param color
		 * 
		 */
		private function drawBG(color:uint=0xfcfcfc):void
		{
			_bgSpace.graphics.clear();
			//以后会不会有背景颜色需求？？暂时不需要
			_bgSpace.graphics.beginFill(color,0);
			if(!_itemHeight)
			{
				_itemHeight	=	20;
			}
			_bgSpace.graphics.drawRect(0,0,_itemWidth,_itemHeight);
			_bgSpace.graphics.endFill();
		}
		
		public function get txtLabel():Label
		{
			return _txtLabel;
		}
		
		/**
		 * 画选中 
		 */
		public function drawHotArea():void
		{ 
			_hotArea.graphics.clear();
			_hotArea.graphics.beginFill(0xff0000,0);
			_hotArea.graphics.drawRect(0,0,_itemWidth,_itemHeight);
			_hotArea.graphics.endFill();
		}
		
		/**
		 * 放置文字(居中)
		 * 
		 */
		private function alignTxt():void
		{
			_txtFeild.x	= (_itemWidth-_txtFeild.width)/2;
		}
		
		/**
		 * 放置文字(居左)
		 * 
		 */
		public function alignLeft():void
		{
			_layOut = "left";
			_txtFeild.x	= 0;
		}
		
		
		/**
		 * 鼠标移过 
		 * @param e
		 * 
		 */
		private function showOver(e:MouseEvent):void
		{
			currentState	=	ButtonState.OVER;
		}
		
		/**
		 * 鼠标移出 
		 * @param e
		 * 
		 */
		private function showOut(e:MouseEvent):void
		{
			currentState = ButtonState.NORMAL;
		}
		
		/**
		 * 鼠标按下 
		 * @param e
		 * 
		 */
		private function showDown(e:MouseEvent):void
		{
			currentState = ButtonState.DOWN;
		}
		
		/**
		 * 鼠标弹起 
		 * @param e
		 * 
		 */
		private function showUP(e:MouseEvent):void
		{
			currentState = ButtonState.OVER;
		}
		/**
		 * 选中 
		 * @param e
		 * 
		 */
		public function selectItem(e:ListItem):void
		{
			clearBG();
			_bgSpace.addChild(_hotArea);
		}
		
		/**
		 * 清背景 
		 */
		private function clearBG():void
		{
			while(_bgSpace.numChildren>0)
			{
				_bgSpace.removeChildAt(0);
			}
		}
		
		override public function set width(val:Number):void
		{
			_itemWidth		=	val;
			drawBG();
			resizeSkin();
			if(_layOut=="center")
			{
				alignTxt();
			}
			if(_layOut=="left")
			{
				alignLeft();
			}
			
//			drawHotArea();
		}
		override public function set height(val:Number):void
		{
			_itemHeight		=	val;
			drawBG();
			resizeSkin();
			if(_layOut=="center")
			{
				alignTxt();
			}
			if(_layOut=="left")
			{
				alignLeft();
			}
//			drawHotArea();
		}
		
		public function set color(val:String):void
		{
			_txtLabel.color = val;
			_txtLabel.htmlStrByBorder = _label;
		}
		
		/**
		 * 重设皮肤 
		 * 
		 */
		private function resizeSkin():void
		{
			if(_skin!="")
			{
				_normalMC.width	=	_overMC.width	=	_downMC.width	=	_unallowed.width	=	_itemWidth;
				_normalMC.height=	_overMC.height	=	_downMC.height	=	_unallowed.height	=	_itemHeight;
			}
		}

		public function get skin():String
		{
			return _skin;
		}

		public function set skin(value:String):void
		{
			clearBG();
			_skin = value;
			if(_skin=="")
			{
				drawBG();
			}
			else
			{
				_bgSpace.graphics.clear();
				
				var res:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
				_normalMC			=	res.getChildByName("Button_normal") as MovieClip;
				_overMC			=	res.getChildByName("Button_over") as MovieClip;
				_downMC			=	res.getChildByName("Button_down") as MovieClip;
				_unallowed 		= 	res.getChildByName("Button_unallowed") as MovieClip;
				resizeSkin();
				_normalMC.x		=	_overMC.x	=	_downMC.x	=	_unallowed.x	=	0;
				_normalMC.y		=	_overMC.y	=	_downMC.y	=	_unallowed.y	=	0;
				_bgSpace.addChild(_normalMC);
			}
		}

		public function set icon(value:DisplayObject):void
		{
			while(_iconSpace.numChildren>0)_iconSpace.removeChildAt(0);
			_icon	=	value;
			if(_icon!=null)
			{
				_iconSpace.addChild(_icon);
			}
		}
		
		/**
		 * 清空
		 */
		private function clearAll(space:Sprite):void
		{
			if(space == null)return; 
			while(space.numChildren > 0){
				space.removeChildAt(0);	
			}
		}
		
		/**
		 * 更新按钮状态显示皮肤
		 */
		private function updataBtnState():void
		{
			clearAll(_bgSpace);
			drawBG();
			var stateSkin:MovieClip;
			switch(_currentState){
				case ButtonState.NORMAL:
					stateSkin = _normalMC;
					break;
				case ButtonState.OVER:
					stateSkin = _overMC;
					break;
				case ButtonState.DOWN:
					stateSkin = _downMC;
					break;
				case ButtonState.UNALLOWED:
					stateSkin = _unallowed;
					break;
			}
			if(_skin)
			{
				_bgSpace.graphics.clear();
				_bgSpace.addChild(stateSkin);
			}
			else if(_currentState==ButtonState.UNALLOWED)
			{
				drawBG(0x333333);
			}
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}

		public function get label():String
		{
			return _label;
		}

		public function set label(value:String):void
		{
			_label = value;
			_txtFeild.htmlText	=	_label;
			if(_layOut=="center")
			{
				alignTxt();
			}
			if(_layOut=="left")
			{
				alignLeft();
			}
		}		
	
		public function get listData():Object
		{
			return _listData;
		}

		public function set listData(value:Object):void
		{
			_listData = value;
		}

		public function get enabled():Boolean
		{
			return _enabled;
		}

		/**
		 * 设置选项状态 
		 * @param value
		 * 
		 */
		public function set enabled(value:Boolean):void
		{
			_enabled = value;
			if(! _enabled){
				currentState = ButtonState.UNALLOWED;
				removeEvent();
				this.mouseEnabled = this.mouseChildren = false;
			}else{
				currentState = ButtonState.NORMAL;
				initEvent();
				this.mouseEnabled = this.mouseChildren = true;
			}
		}

		public function get currentState():String
		{
			return _currentState;
		}

		public function set currentState(value:String):void
		{
			_currentState = value;
			this.width	=	this.width;
			updataBtnState();
		}


	}
}